/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.senac.maisconhecimento.servlet.livros;

import br.senac.maisconhecimento.dao.*;
import br.senac.maisconhecimento.entidade.*;
import br.senac.maisconhecimento.servlet.BaseServlet;
import br.senac.maisconhecimento.viewdata.ListarLivrosViewData;
import br.senac.maisconhecimento.viewdata.ListarLivrosViewData;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Marcus Lemos
 */
@WebServlet(name = "ListarLivrosServlet", urlPatterns = {"/Livros"})
public class ListarLivrosServlet extends BaseServlet {

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        /*
         * UsuarioDAO userDao = new UsuarioDAO(); Usuario user =
         * userDao.consultarPeloCodigo(2); HttpSession sessionRequest =
         * request.getSession(); sessionRequest.setAttribute("usuarioLogado", user);
         */

        HttpSession sessionRequest = request.getSession();
        Object objEmSessao = sessionRequest.getAttribute("usuarioLogado");

        if (objEmSessao == null) {
            RequestDispatcher rd = request.getRequestDispatcher("Login");
            rd.forward(request, response);
        } else {
            int isbn = RetornarIsbn(request);
            String nome = request.getParameter("nome");;
            int codigoCategoria = RetornarCodigoCategoria(request);
            String autor = request.getParameter("autor");;

            LivroDAO livroDao = new LivroDAO();
            List<Livro> livros = livroDao.filtrarAtivos(isbn, codigoCategoria, nome, autor);

            ListarLivrosViewData viewData = new ListarLivrosViewData(request, livros);
            request.setAttribute("ListarLivrosViewData", viewData);

            RequestDispatcher rd = request.getRequestDispatcher("livros/index.jsp");
            rd.forward(request, response);
        }
    }

    private int RetornarIsbn(HttpServletRequest request) {
        try {
            return Integer.parseInt(request.getParameter("isbn"));
        } catch (Exception e) {
            return 0;
        }
    }

    private int RetornarCodigoCategoria(HttpServletRequest request) {
        try {
            return Integer.parseInt(request.getParameter("categoria"));
        } catch (Exception e) {
            return 0;
        }
    }
}
